package com.car.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailUtils {
	
	
	 public static void sendEmail(String to,String subject,String content,String nick) throws IOException, AddressException, MessagingException{

	        Properties prop = new Properties();
	        InputStream resourceAsStream = null;
	        try {
	             resourceAsStream = MailUtils.class.getResourceAsStream("../../../email.properties");
	             prop.load(resourceAsStream);
	        } finally{
	            if (resourceAsStream!=null) {
	                resourceAsStream.close();
	            }
	        }
	        prop.setProperty("mail.host", "smtp.qq.com");
	        prop.setProperty("mail.transport.protocol", "smtp");
	        prop.setProperty("mail.smtp.auth", "true");
	        prop.setProperty("mail.smtp.starttls.enable", "true");
	        Authenticator authenticator = new Email_Authenticator(prop.get("username").toString(), prop.get("password").toString());
	        javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(prop, authenticator);
	        MimeMessage mailMessage = new MimeMessage(sendMailSession);
	        mailMessage.setFrom(new InternetAddress(javax.mail.internet.MimeUtility.encodeText(nick)+" <"+  prop.get("username").toString()+">"));
	        // Message.RecipientType.TO属性表示接收者的类型为TO
	        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
	        mailMessage.setSubject(subject, "UTF-8");
	        mailMessage.setSentDate(new Date());
	        // MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象
	        Multipart mainPart = new MimeMultipart();
	        // 创建一个包含HTML内容的MimeBodyPart
	        BodyPart html = new MimeBodyPart();
	        html.setContent(content.trim(), "text/html; charset=utf-8");
	        mainPart.addBodyPart(html);
	        mailMessage.setContent(mainPart);
	        Transport.send(mailMessage);
	    }
	 	/**
	     * 正则表达式校验邮箱
	     * @param emaile 待匹配的邮箱
	     * @return 匹配成功返回true 否则返回false;
	     */
	   public static boolean checkEmaile(String emaile){
	        String RULE_EMAIL = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
	        //正则表达式的模式
	        Pattern p = Pattern.compile(RULE_EMAIL);
	        //正则表达式的匹配器
	        Matcher m = p.matcher(emaile);
	        //进行正则匹配
	        return m.matches();
	    }
	}

class Email_Authenticator extends Authenticator {
    String userName = null;
    String password = null;
    public Email_Authenticator() {
    }
    public Email_Authenticator(String username, String password) {
        this.userName = username;
        this.password = password;
    }
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userName, password);
    }
}